<template>
	<view class="create-order">
		<y-cubar title="提交订单" :bgColor="`rgba(255,255,255,${opacity})`" />
		<view class="create-order-bg"></view>
		<view class="address-card">
			<view class="tabs">
				<view class="tab-w" :class="{ 'tab-w-a' : current === 0}" @click="tabChange(0)">
					<view class="tab" :class="{ 'tab-b' : current === 1}">
						<text>上门自提</text>
					</view>
				</view>
				<view class="tab-w" :class="{ 'tab-w-a' : current === 1}" @click="tabChange(1)">
					<view class="tab" :class="{ 'tab-a' : current === 0}">
						<text>送车上门</text>
					</view>
				</view>
			</view>
			<view class="address-card-main">
				<template v-if="current === 0">
					<view class="shop-info">
						<view class="info">
							<view class="address">{{ info.province }}{{ info.city }}{{ info.address }}</view>
							<view class="phone">
								<text>预留电话</text>
								<text>{{ info.cellphone }}</text>
							</view>
						</view>
					</view>
				</template>
				<template v-else>
					<view class="shop-info">
						<view class="info">
							<template v-if="queryForm.provinceName">
								<view class="" @click="handChooseAddress">
									<view class="flex align-center">
										<view class="address" style="flex: 1; padding-right: 20rpx;">
											{{ queryForm.provinceName }}{{ queryForm.cityName }}{{ queryForm.address }}
										</view>
										<uv-icon name="arrow-right"></uv-icon>
									</view>
									<view class="phone">
										<text>{{ queryForm.name }}</text>
										<text>{{ queryForm.phone }}</text>
									</view>
								</view>
							</template>
							<template v-else>
								<view class="address-empty" @click="handChooseAddress">
									<image
										src="https://wuyeshop.oss-cn-beijing.aliyuncs.com/image/20230712/20230712100849_bqb2.png"
										mode=""></image>
									<text>暂无地址，去添加吧~</text>
								</view>
							</template>
						</view>
					</view>
				</template>
			</view>
		</view>
		<view class="card">
			<view class="title">车辆信息</view>
			<view class="car-info">
				<image :src="info.coverPlan" mode="aspectFill"></image>
				<view class="info">
					<view class="name">{{ info.productName }}</view>
					<view class="desc">
						<text>{{ info.brand }}{{ info.model }}</text>
					</view>
					<view class="tags">
						<view class="tag" v-if="info.powerType">{{ info.powerType }}</view>
						<view class="tag" v-if="info.mileage">{{ info.mileage }}</view>
						<view class="tag" v-if="info.fineness">{{ info.fineness }}</view>
						<view class="tag" v-if="info.chargingMode">{{ info.chargingMode }}</view>
					</view>
				</view>
			</view>
			<view class="grid">
				<view class="grid-item">
					<view class="value">
						<text>{{ queryForm.schemes.lease }}</text>
						<text>个月</text>
					</view>
					<view class="label">期数</view>
				</view>
				<view class="grid-item">
					<view class="value">
						<text>{{ queryForm.schemes.rent }}</text>
						<text>元/月</text>
					</view>
					<view class="label">价格</view>
				</view>
				<view class="grid-item">
					<view class="value">
						<text>{{ queryForm.schemes.deposit }}</text>
						<text>元</text>
					</view>
					<view class="label">押金(可退)</view>
				</view>
			</view>
		</view>
		<view class="card">
			<view class="title">商家信息</view>
			<view class="enum">
				<view class="cell">
					<view class="label">商家类型</view>
					<view class="value type" v-if="info.type==='PROPRIETARY'">平台自营</view>
					<view class="value type" v-else>车商</view>
				</view>
				<view class="cell">
					<view class="label">商家名称</view>
					<view class="value">{{ info.merchantName }}</view>
				</view>
				<view class="cell">
					<view class="label">联系人</view>
					<view class="value">{{ info.name }}</view>
				</view>
				<view class="cell">
					<view class="label">联系电话</view>
					<view class="value">{{ info.cellphone }}</view>
				</view>
			</view>
		</view>
		<view class="card">
			<view class="title">租赁人信息</view>
			<view class="enum">
				<view class="cell">
					<view class="label">联系人</view>
					<view class="value">{{ queryForm.budgetName }}</view>
				</view>
				<view class="cell">
					<view class="label">联系电话</view>
					<view class="value">{{ queryForm.cellphone }}</view>
				</view>
				<view class="cell">
					<view class="label">合同</view>
					<view class="value ht" @click="handPrive">查看</view>
				</view>
			</view>
		</view>
		<view class="action-bottom">
			<view class="action-bottom-left">
				<text v-if="info.type==='PROPRIETARY'">支付押金</text>
				<text v-else>支付金额</text>
				<text>￥</text>
				<text>{{ price }}</text>
			</view>
			<button class="cu-btn" @click="handSubmit">提交订单</button>
		</view>
	</view>
</template>

<script setup>
	import {
		ref,
		getCurrentInstance,
		computed
	} from 'vue'
	import {
		onLoad,
		onPageScroll
	} from "@dcloudio/uni-app";
	import {
		doDetail
	} from '@/apis/car.js'
	import {
		doGetDefault
	} from '@/apis/address.js'
	import {
		doCreateOrder
	} from '@/apis/shop.js'
	import YCubar from '@/components/y-cubar/index.vue'

	const {
		proxy
	} = getCurrentInstance();

	const queryForm = ref({
		schemes: {}
	})
	const handSubmit = async () => {
		const params = uni.$uv.deepClone(queryForm.value)
		params.type = current.value === 0 ? 'EXTRACT' : 'SEND'
		if (params.type === 'SEND' && !params.provinceName) {
			uni.$uv.toast('请选择地址')
			return
		}
		params.schemes = JSON.stringify(params.schemes)
		uni.showLoading({
			title: '正在生成订单...'
		})
		const {
			code,
			message
		} = await doCreateOrder(params)
		if (code !== 200) {
			uni.hideLoading()
			uni.$uv.toast(message)
			return
		}
		const eventChannel = proxy.getOpenerEventChannel()
		eventChannel.emit('success');
		handPay(JSON.parse(message))
	}
	const handPay = (payInfo) => {
		uni.hideLoading()
		const orderInfo = {
			provider: 'wxpay', // 服务提提供商
			timeStamp: payInfo.timeStamp, // 时间戳
			nonceStr: payInfo.nonceStr, // 随机字符串
			package: payInfo.package,
			signType: payInfo.signType, // 签名算法
			paySign: payInfo.paySign, // 签名
		};
		uni.requestPayment({
			provider: 'wxpay',
			...orderInfo,
			success: (res) => {
				uni.$uv.toast('支付成功')
				setTimeout(() => {
					uni.switchTab({
						url: '/pages/tabbar/order/index'
					})
				}, 500)
			},
			fail: (err) => {
				uni.$uv.toast('支付失败')
				uni.switchTab({
					url: '/pages/tabbar/order/index'
				})
			}
		});
	}
	const price = computed(() => {
		if (info.value?.id) {
			if (info.value.type === 'PROPRIETARY') {
				return Number(queryForm.value.schemes.deposit)
			} else {
				return Number(queryForm.value.schemes.deposit) + Number(queryForm.value.schemes.rent)
			}
		}
		return ''
	})
	const info = ref({
		strength: {},
		slideshow: [],
		schemes: [],
		detailsImg: [],
		configure: []
	})
	onLoad(() => {
		const eventChannel = proxy.getOpenerEventChannel()
		eventChannel.on('editData', (data) => {
			init(data)
		})
	})

	const init = async (newInfo) => {
		queryForm.value = newInfo
		const data = await doDetail(newInfo.productId)
		if (data.platform) {
			data.platform = JSON.parse(data.platform)
		} else {
			data.platform = []
		}
		data.platformName = uni.$uv.deepClone(data.platform).map(el => {
			return el.name
		}).toString()
		if (data.schemes) {
			data.schemes = JSON.parse(data.schemes)
		} else {
			data.schemes = []
		}
		if (data.strength) {
			data.strength = JSON.parse(data.strength)
		} else {
			data.strength = {}
		}
		info.value = data
		doGetDefault().then(res => {
			if (res) {
				queryForm.value.provinceName = res.provinceName
				queryForm.value.cityName = res.cityName
				queryForm.value.address = res.address
				queryForm.value.name = res.name
				queryForm.value.phone = res.cellphone
			}
		})
	}

	const handPrive = () => {
		uni.navigateTo({
			url: '/pagesA/car/contract?type=prive',
			success: (res) => {
				res.eventChannel.emit('acceptDataFromOpenerPage', queryForm.value.htInfo)
			}
		})
	}

	const handChooseAddress = () => {
		uni.navigateTo({
			url: '/pagesA/address/list?type=choose',
			events: {
				someEvent: (res) => {
					queryForm.value.provinceName = res.provinceName
					queryForm.value.cityName = res.cityName
					queryForm.value.address = res.address
					queryForm.value.name = res.name
					queryForm.value.phone = res.cellphone
				}
			}
		})
	}

	const opacity = ref(0)
	onPageScroll((e) => {
		const childerOpa = Number((e.scrollTop / 200).toFixed(2))
		opacity.value = childerOpa >= 1 ? 1 : childerOpa
	})

	const current = ref(0)
	const tabChange = (e) => {
		current.value = e
	}
</script>

<style lang="scss" scoped>
	.create-order {
		min-height: 100vh;
		background-color: #F5F5F5;
		padding-bottom: 150rpx;
		padding-bottom: calc(constant(safe-area-inset-bottom) + 150rpx);
		padding-bottom: calc(env(safe-area-inset-bottom) + 150rpx);

		&-bg {
			width: 750rpx;
			height: 408rpx;
			background: linear-gradient(180deg, #F7C241 0%, #F5F5F5 100%);
			position: absolute;
			top: 0;
		}

		.address-card {
			position: relative;
			z-index: 2;
			width: 702rpx;
			margin: 30rpx auto 0 auto;
			box-sizing: border-box;

			.tabs {
				display: flex;
				align-items: flex-end;
				overflow: hidden;
				border-radius: 20rpx 20rpx 0 0;

				.tab-w {
					flex: 1;
					height: 80rpx;
					background-color: #FFFFFF;
					display: flex;
					align-items: flex-end;
					background-color: #FFFFFF;
					border-radius: 20rpx 20rpx 0 0;

					.tab {
						width: 100%;
						background-color: #FFF7E4;
						height: 80rpx;
						line-height: 80rpx;
						text-align: center;
						font-size: 28rpx;
						font-family: PingFang SC-Regular, PingFang SC;
						font-weight: 400;
						color: #333333;
						position: relative;

						&-a {
							border-radius: 0 20rpx 0 30rpx;
						}

						&-b {
							border-radius: 20rpx 0 30rpx 0;
						}
					}

					&-a {
						height: 100rpx;
						background-color: #FFFFFF;
						border-radius: 0;

						.tab {
							background-color: #FFFFFF;
							line-height: 100rpx;
							line-height: 100rpx;
							font-size: 32rpx;
							font-weight: bold;
							bottom: 15rpx;
						}
					}

				}
			}

			&-main {
				background-color: #FFFFFF;
				border-radius: 0 0 20rpx 20rpx;

				&-radius {
					border-radius: 20rpx;
				}

				.address-empty {
					padding: 44rpx 24rpx;
					display: flex;
					flex-direction: column;
					align-items: center;
					justify-content: center;

					image {
						width: 68rpx;
						height: 68rpx;
					}

					text {
						margin-top: 12rpx;
						font-size: 24rpx;
						font-family: PingFang SC-Regular, PingFang SC;
						font-weight: 400;
						color: #999999;
					}
				}

				.shop-info {
					padding: 44rpx 24rpx;

					.info {
						margin-right: 20rpx;

						.address {
							font-size: 32rpx;
							font-family: PingFang SC-Bold, PingFang SC;
							font-weight: bold;
							color: #333333;
						}

						.phone {
							margin-top: 24rpx;
							font-size: 28rpx;
							font-family: PingFang SC-Regular, PingFang SC;
							font-weight: 400;
							color: #666666;
							display: flex;
							align-items: center;
							justify-content: space-between;
						}
					}
				}
			}
		}

		.card {
			width: 702rpx;
			margin: 30rpx auto 0 auto;
			box-sizing: border-box;
			background-color: #fff;
			border-radius: 16rpx;
			padding: 24rpx;

			.title {
				font-size: 32rpx;
				font-family: PingFang SC-Bold, PingFang SC;
				font-weight: bold;
				color: #333333;
			}

			.car-info {
				margin-top: 24rpx;
				display: flex;
				align-items: center;

				image {
					width: 160rpx;
					height: 160rpx;
					border-radius: 16rpx;
				}

				.info {
					flex: 1;
					margin-left: 24rpx;

					.name {
						font-size: 30rpx;
						font-family: PingFang SC-Bold, PingFang SC;
						font-weight: bold;
						color: #333333;
					}

					.desc {
						margin-top: 8rpx;
						font-size: 24rpx;
						font-family: PingFang SC-Regular, PingFang SC;
						font-weight: 400;
						color: #999999;
					}

					.tags {
						margin-top: 14rpx;
						display: flex;

						.tag {
							font-size: 24rpx;
							font-family: PingFang SC-Regular, PingFang SC;
							font-weight: 400;
							color: #999999;
							padding: 6rpx 12rpx;
							background: #F5F5F5;
							border-radius: 4rpx;

							&:not(:first-child) {
								margin-left: 8rpx;
							}
						}
					}
				}
			}

			.grid {
				margin-top: 32rpx;
				background: #F8F8F8;
				border-radius: 16rpx;
				padding: 32rpx 40rpx;
				display: flex;
				justify-content: space-between;

				&-item {
					.value {
						text {
							font-size: 24rpx;
							font-family: PingFang SC-Regular, PingFang SC;
							font-weight: 400;
							color: #FF4E00;

							&:first-child {
								font-size: 32rpx;
							}
						}
					}

					.label {
						margin-top: 10rpx;
						font-size: 26rpx;
						font-family: PingFang SC-Regular, PingFang SC;
						font-weight: 400;
						color: #666666;
					}
				}
			}

			.enum {
				margin-top: 24rpx;

				.cell {
					display: flex;
					justify-content: space-between;

					&:not(:first-child) {
						margin-top: 32rpx;
					}

					.label {
						font-size: 30rpx;
						font-family: PingFang SC-Regular, PingFang SC;
						font-weight: 400;
						color: #999999;
					}

					.value {
						flex: 1;
						margin-left: 40rpx;
						font-size: 30rpx;
						font-family: PingFang SC-Regular, PingFang SC;
						font-weight: 400;
						color: #333333;
						text-align: right;
					}

					.type {
						color: #F57B01;
					}

					.ht {
						color: #2758E7;
					}
				}
			}
		}

		.action-bottom {
			position: fixed;
			bottom: 0;
			width: 750rpx;
			background-color: #fff;
			padding: 18rpx 24rpx;
			box-shadow: 0rpx -2rpx 12rpx 2rpx rgba(0, 0, 0, 0.03);
			padding-bottom: 18rpx;
			padding-bottom: calc(constant(safe-area-inset-bottom) + 18rpx);
			padding-bottom: calc(env(safe-area-inset-bottom) + 18rpx);
			display: flex;
			align-items: center;
			justify-content: space-between;

			&-left {
				font-size: 26rpx;
				font-family: PingFang SC-Medium, PingFang SC;
				font-weight: 500;
				color: #333333;

				text {
					&:nth-child(2) {
						margin-left: 10rpx;
						color: #FF4E00;
					}

					&:last-child {
						font-size: 48rpx;
						font-weight: bold;
						color: #FF4E00;
					}
				}
			}

			.cu-btn {
				width: 288rpx;
				height: 100rpx;
				background: #FFC434;
				border-radius: 16rpx;
				font-size: 32rpx;
				font-family: PingFang SC-Bold, PingFang SC;
				font-weight: bold;
				color: #333333;
			}
		}
	}
</style>